﻿namespace Ex03.GarageLogic
{
    using System;
    using System.Collections.Generic;
    using System.Text;

    public class ElectricPropulsion
    {
        private const float k_MinimalEnergyAmountInBattery = 0.0F;
        public const float k_MinutesInHour = 60.0F;

        private EnergyType.eEnergyType m_BatteryType = EnergyType.eEnergyType.DefaultValue;
        private float m_BatteryPowerLeftInHours = 0.0F;
        private float m_BatteryMaximumPowerInHours = 0.0F;

        public ElectricPropulsion(
                EnergyType.eEnergyType i_PropulsionBatteryType, 
                float i_BatteryMaximumPowerInHours)
        {
            if (isMaximumBatteryValueLegal(i_BatteryMaximumPowerInHours) && isEnergyTypeLegal(i_PropulsionBatteryType))
            {
                m_BatteryType = i_PropulsionBatteryType;
                m_BatteryMaximumPowerInHours = i_BatteryMaximumPowerInHours;
            }
        }
        
        private bool isEnergyTypeLegal(EnergyType.eEnergyType i_PropulsionBatteryType)
        {
            bool ReturnValue = false;

            switch (i_PropulsionBatteryType)
            { 
                case EnergyType.eEnergyType.NiMHBattery:
                    ReturnValue = true;
                    break;
                case EnergyType.eEnergyType.Octan95:
                case EnergyType.eEnergyType.Octan96:
                case EnergyType.eEnergyType.Octan98:
                case EnergyType.eEnergyType.Soler:
                case EnergyType.eEnergyType.DefaultValue:
                default:
                    break;
            }

            if (ReturnValue == false)
            {
                ArgumentException IllegalBatteryTypeError = new ArgumentException();

                throw IllegalBatteryTypeError;
            }

            return ReturnValue;
        }

        private bool isMaximumBatteryValueLegal(float i_BatteryMaximumPowerInHours)
        {
            bool ReturnValue = false;

            if (i_BatteryMaximumPowerInHours <= k_MinimalEnergyAmountInBattery)
            {
                ReturnValue = true;
            }
            else
            {
                ValueOutOfRangeException BatteryPowerSizeError = new ValueOutOfRangeException();

                throw BatteryPowerSizeError;
            }

            return ReturnValue;
        }

        public EnergyType.eEnergyType BatteryType
        {
            get { return m_BatteryType; }
            set
            {
                if (isEnergyTypeLegal(value))
                {
                    m_BatteryType = value;
                }
            
            }
        }

        public float CurrentPowerLevelInBattery
        {
            get { return m_BatteryPowerLeftInHours; }
            set
            {
                if (isPowerSettingLevelLegalForBattery(value))
                {
                    m_BatteryPowerLeftInHours = value;
                }
            }
        }

        public bool isPowerSettingLevelLegalForBattery(float i_PowerAmountInput)
        {
            bool ReturnValue = false;

            if ((i_PowerAmountInput <= MaximumPowerInBatteryInHours) &&
                (i_PowerAmountInput >= k_MinimalEnergyAmountInBattery))
            {
                ReturnValue = true;
            }
            else
            {
                ValueOutOfRangeException EnerghInputError = new ValueOutOfRangeException();

                throw EnerghInputError;
            }

            return ReturnValue;
        }

        public float MaximumPowerInBatteryInHours
        {
            get { return m_BatteryMaximumPowerInHours; }
            set
            {
                if (isMaximumBatteryValueLegal(value))
                {
                    m_BatteryMaximumPowerInHours = value;
                }
            }
        }

        public bool WouldBatteryOvercharge(float i_PowerAmountToRecharge)
        {
            bool ReturnValue = false;

            if (i_PowerAmountToRecharge + m_BatteryPowerLeftInHours <= m_BatteryMaximumPowerInHours)
            {
                ReturnValue = true;
            }
            else
            {
                ValueOutOfRangeException BatteryOverchargeError = new ValueOutOfRangeException();

                throw BatteryOverchargeError;
            }

            return ReturnValue;
        }

        public bool AddPropulsionMaterial(
            EnergyType.eEnergyType i_PropulsionMaterialType, 
            float i_PropulsionMaterialAmountToAdd)
        {
            bool ReturnValue = false;

            if (isEnergyTypeLegal(i_PropulsionMaterialType) && 
                InternalCombustionPropulsion.DoesFuelTypesMatch(m_BatteryType, i_PropulsionMaterialType) &&
                WouldBatteryOvercharge(i_PropulsionMaterialAmountToAdd))
            {
                m_BatteryPowerLeftInHours += i_PropulsionMaterialAmountToAdd;
                ReturnValue = true;
            }

            return ReturnValue;
        }

        // see the function AddPropulsionMaterial
        //public bool RechargeBattery(float i_PowerAmountToRecharge, EnergyType.eEnergyType i_BatteryType)
        //{
        //    bool ReturnValue = false;

        //    if ((m_BatteryType == i_BatteryType) && !WouldBatteryOvercharge(i_PowerAmountToRecharge))
        //    {
        //        ReturnValue = true;
        //    }

        //    return ReturnValue;
        //}

        //public void RefuelEnergySource(float i_MinutesToRecharge, )
        //{
        //    if (m_EngineEnergyType.RechargeBattery(
        //}
    }
}
